<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>添加文章类别</title>
    <link href="${ctx }/css/ajax_tip.css" rel="stylesheet" type="text/css" />
    <link href="${ctx }/css/ztreeStyle.css" rel="stylesheet" type="text/css" />
    <link rel="stylesheet" href="${ctx }/js/layui/css/layui.css " media="all"/>
    <link href="${ctx }/css/ztree/zTreeStyle/zTreeStyle.css" rel="stylesheet" type="text/css" />

    <script type="text/javascript" src="${ctx }/js/jquery/jquery-1.8.2.min.js"></script>
    <script type="text/javascript" src="${ctx }/js/layer/layer.js"></script>
    <script type="text/javascript" src="${ctx }/js/niceValidator/jquery.validator.js"></script>
    <script type="text/javascript" src="${ctx }/js/niceValidator/local/zh_CN.js"></script>
    <script type="text/javascript" src="${ctx }/js/ztree/jquery.ztree.core-3.5.js"></script>
    <script type="text/javascript" src="${ctx}/js/common.js"></script>
    <script type="text/javascript" src="${ctx }/js/layui/layui.js"></script>
</head>
<body>
<div style="width:281px;height:100%;float:left;margin:8px;">
    <table class="tablelist">
        <thead>
        <tr>
            <th>文章类别</th>
        </tr>
        </thead>
        <tbody>
        <tr>
            <td>
                <div class="zTreeDemoBackground left" style="overflow-y: scroll;height: 300px;">
                    <ul id="treeDemo" class="ztree areaul"></ul>
                </div>
            </td>
        </tr>
        </tbody>
    </table>
    <center style="margin:5px;">
        <button class="layui-btn layui-btn-normal" id="clearArticleType" type="button">重置</button>
    </center>
</div>

</body>
</html>
<script>
    $(document).ready(function(){
        onloadNav();

        $("#clearArticleType").click(function(){
            parent.$("#typeId").val("");
            parent.$("#typeName").val("");
            var index = parent.layer.getFrameIndex(window.name);
            window.parent.layer.close(index);
        });
    });
    var articleTypeList = ${articleTypeList};
    var areaZtree,areaMenu;
    // 加载权限资源图数据
    function onloadNav(){
        var loadi = layer.load();
        var setting = {
            data: {
                simpleData: {
                    enable: true
                }
            },
            async: {
                enable: true,
                url:"${ctx}/sysman/articles/popupArticleTypeList.do",
                autoParam:["id=parentId"],
                dataFilter: filter
            },
            callback: {
                onClick: onClick

            }
        };
        if(panIsNull(articleTypeList)){
            layer.alert('未找到文章类别数据');
            $("#treeDemo").html('<div align="center" style="color: gray">无数据</div>');
            layer.close(loadi);
            return;
        }
        var jsondata="[";
        for(var i = 0;i<articleTypeList.length;i++){
            jsondata+="{'pId':'"+articleTypeList[i].parentid+"','id':'"+articleTypeList[i].id+"','name':'"+articleTypeList[i].name+"'},"
        }
        jsondata = jsondata.substring(0,jsondata.length-1);
        jsondata+="]";
        jsondata=eval(jsondata);
        $.fn.zTree.init($("#treeDemo"), setting, jsondata);
        areaZtree = $.fn.zTree.getZTreeObj("treeDemo");
        areaMenu = $("#areaForOrgansMenu");
        layer.close(loadi);
    }

    function filter(treeId, parentNode, childNodes) {
        if (!childNodes){
            /*parent.$("#articleTypeId").val(parentNode.id);
            parent.$("#articleTypeName").text(parentNode.name);
            var index = parent.layer.getFrameIndex(window.name);
            window.parent.layer.close(index);*/
            return null;
        }
        if (childNodes.length==0){
            parent.$("#typeId").val(parentNode.id);
            parent.$("#typeName").val(parentNode.name);
            var index = parent.layer.getFrameIndex(window.name);
            window.parent.layer.close(index);
        }
        var jsondata="[";
        for(var i = 0;i<childNodes.length;i++){
            jsondata+="{'pId':'"+childNodes[i].parentId+"','id':'"+childNodes[i].id+"','name':'"+childNodes[i].name+"','id':'"+childNodes[i].id+"','type':'articleType'},"
        }
        jsondata = jsondata.substring(0,jsondata.length-1);
        jsondata+="]";
        jsondata=eval(jsondata);
        return jsondata;
    }
    var type = '';
    function refreshNodes(treeNode) {
        var zTree = $.fn.zTree.getZTreeObj("treeDemo");
        if(type.indexOf(treeNode.id)<0) {
            type += "," + treeNode.id;
            treeNode.isParent = true;
            zTree.reAsyncChildNodes(treeNode, 'refresh', false);
        }
    }

    function onClick(event, treeId, treeNode, clickFlag) {
        //parent.$("#articleTypeId").val(treeNode.id);
        //parent.$("#articleTypeName").text(treeNode.name);
        refreshNodes(treeNode);
        //baseSearchQuery(1);
    }
</script>